package etc

import "strings"
import "go.bug.st/serial/enumerator"

var Config Configuration

const (
	CH34x = "ch34x"
	FT2x = "ft23x"
	CP2x = "cp21x"
	PL = "pl23x"
)

var vendor = make(map[string][]string)

func init() {
	vendor[CH34x] = []string{"1a86"}
	vendor[FT2x] = []string{"0403", "165c"}
	vendor[CP2x] = []string{"10c4"}
	vendor[PL] = []string{"067b"}
}

type Uart struct {

	Name string
	SerialNumber string
	Vendor string
	Product string
}

func SerialPorts() (list []Uart, err error) {
	ports, err := enumerator.GetDetailedPortsList()
	if err != nil {
		return
	}

	for _, port := range ports {
		if port.IsUSB {
			uart := Uart{
				Name:         port.Name,
				SerialNumber: port.SerialNumber,
				Vendor:       strings.ToLower(port.VID),
				Product:      strings.ToLower(port.PID),
			}
			list = append(list, uart)
		}
	}
	return
}

func IsReader(uart Uart) bool {

	if list, ok := vendor[Config.Connector.Vendor]; ok {
		for _, ven := range list {
			if uart.Vendor == ven {
				return true
			}
		}
	} else {
		return false
	}
	return false
}